package logic

import (
	"context"
	"errors"
	"video-hub/model/mysql"

	"video-hub/videorpc/internal/svc"
	"video-hub/videorpc/videorpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CollectDelLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCollectDelLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CollectDelLogic {
	return &CollectDelLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CollectDelLogic) CollectDel(in *videorpc.CollectDelRequest) (*videorpc.CollectDelResponse, error) {
	// todo: add your logic here and delete this line
	var collect mysql.Collect
	var video mysql.Video

	err := collect.CollectDel(mysql.DB, in.CollectId)
	if err != nil {
		return nil, errors.New("删除失败")
	} else {
		err = collect.FindCollectId(mysql.DB, in.CollectId)
		if err != nil {
			return nil, err
		}
		if int(video.ID) == collect.VideoId {
			err = video.UdateNums(mysql.DB, video.ID)
			if err != nil {
				return nil, err
			}
		}
	}
	return &videorpc.CollectDelResponse{}, nil
}
